package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.util.DateTools;

@Service
public class SealLogService extends BaseService {
	@Autowired
	private BankService service;

	public List<Map<String, Object>> selectSealLogList(String srh_name,String bank_id,Page page) {
		String sql = "select t.*,ts.uname,tu.uname auth_name from t_seal_log t "
				+ " left outer join t_user ts on  t.user_id=ts._id "
				+ " left outer join t_user tu on  t.auth_user_id=tu._id "
				+ " where t.status=0 and t.bank_id=? and t.seal_code like ? order by t.created desc ";
		List<String> params = new ArrayList<String>();
		
		params.add(bank_id);
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.seal_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(),page);
		return list;
	}
	
	public List<Map<String, Object>> selectSealUseLogList(String srh_bank,String seal_code,String srh_start,String srh_end,Page page) {
		String ids = service.selectUbank2();
		if(ChkTools.isNull(ids)){
			ids = "''";
		}
		String sql = "select t.*,ts.uname,tb.name bname from t_seal_log t "
				+ " left outer join t_user ts on  t.user_id=ts._id "
				+ " left outer join t_bank tb on  t.bank_id=tb._id "
				+ " where t.status=0 and tb._id in (@ids) and tb.name like ? and t.seal_code like ? and t.seal_time>=? and t.seal_time<=?"
				+ " order by t.created desc ";
		List<String> params = new ArrayList<String>();
	
		if (ChkTools.isNull(srh_bank)) {
			sql = sql.replace("and tb.name like ?", "");
		} else {
			params.add("%" + srh_bank + "%");
		}
		if (ChkTools.isNull(seal_code)) {
			sql = sql.replace("and t.seal_code like ?", "");
		} else {
			params.add("%" + seal_code + "%");
		}
		if (ChkTools.isNull(srh_start)) {
			sql = sql.replace("and t.seal_time>=? and t.seal_time<=?", "");
		} else {
			params.add(srh_start);
			params.add(DateTools.addOneDay(srh_end));
		}
		sql = sql.replace("@ids", ids);
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(),page);
		return list;
	}
	
	public static void main(String[] args) {

	}
}
